#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 805;

int n, m;
ll a[N];
ll dp[N][N];

void solve() {
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) a[i + n] = a[i];
  rep(i, 1, n * 2) rep(j, 1, n * 2) dp[i][j] = 0;
  rep(len, 3, n) {
    rep(i, 1, n * 2 + 1 - len) {
      int j = i + len - 1;
      dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]);
      rep(k, i + 1, j - 1) {
        dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
        dp[i][j] = max(
            dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j - 1] + a[i] * a[k] * a[j]);
      }
    }
  }
  ll ans = 0;
  rep(i, 1, n) ans = max(ans, dp[i][i + n - 1]);
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}